﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;
using Viewer.Base.Config;
using Microsoft.Reporting.WebForms;

namespace Viewer.NhanSu.Reporting
{
    public partial class RptThongKeNhanSu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Disable();
            if (!Page.IsPostBack)
            {
                LoadDataComboboxLoai();
            }
        }
        /// <summary>
        /// Generate Report
        /// </summary>
        [DirectMethod]
        public void btnGenerate_Click()
        {
            //if (string.IsNullOrEmpty(txtToChuc.Text))
            //{
            //    hdTochucID.Text = string.Empty;
            //}
            string reportPath = string.Empty;
            string toChucId = !string.IsNullOrEmpty(hdTochucID.Text) ? hdTochucID.Text : string.Empty;

            // Path for Server Report          
            if (!hdReportType.Value.Equals("5"))
            {
                //Check điều kiện trong trường hợp chọn báo cáo danh sách nhân viên
                if (hdReportType.Value.Equals("6") && CheckValidateBaoCaoNhanVien())
                {
                    lblMessageYear_NhanVien.Hidden = true;
                    lblMessageToChuc_NhanVien.Hidden = true;
                    reportPath = ViewerConstant.NameReport.NSBaoCaoNhanVien;
                    string strParameter = string.Format("{0},{1},{2},{3}", reportPath, txtToChuc.Text, toChucId, cbbYear.Text);
                    UCReporting.LoadReport(strParameter);
                }
                else if (!hdReportType.Value.Equals("6"))
                {
                    if (hdReportType.Value.Equals("0"))
                    {
                        reportPath = ViewerConstant.NameReport.NSNghiPhep;
                    }
                    else if (hdReportType.Value.Equals("1"))
                    {
                        reportPath = ViewerConstant.NameReport.NSPhanBoHopDong;
                    }
                    else if (hdReportType.Value.Equals("2"))
                    {
                        reportPath = ViewerConstant.NameReport.NSBacLuong;
                    }
                    else if (hdReportType.Value.Equals("3"))
                    {
                        reportPath = ViewerConstant.NameReport.NSPhuCapChucVu;
                    }
                    else if (hdReportType.Value.Equals("4"))
                    {
                        reportPath = ViewerConstant.NameReport.NSPhuCapKhac;
                    }
                    else if (hdReportType.Value.Equals("7"))
                    {
                        reportPath = ViewerConstant.NameReport.NSTiLeNamNu;
                    }
                    string strParameter = string.Format("{0},{1},{2},{3}", reportPath, txtToChuc.Text, toChucId, cbbYear.Text);
                    UCReporting.LoadReport(strParameter);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(ccbLoai.SelectedItem.Value) && hdReportType.Value.Equals("5"))
                {
                    lblMessage.Hidden = false;
                }
                else
                {
                    lblMessage.Hidden = true;
                    if (CheckValidateThayDoiNhanVien())
                    {
                        reportPath = ViewerConstant.NameReport.NSThayDoiSoLuongNhanVien;

                        string FromMonth = string.Empty;
                        string EndMonth = string.Empty;
                        string FromYear = string.Empty;
                        string EndYear = string.Empty;
                        if (ccbLoai.Value.ToString().Equals("Month"))
                        {
                            FromMonth = cbbFromMonth.Value.ToString();
                            EndMonth = cbbEndMonth.Value.ToString();
                            FromYear = cbbFromYear.Value.ToString();
                            EndYear = cbbEndYear.Value.ToString();
                        }
                        else if (ccbLoai.Value.ToString().Equals("Qui"))
                        {
                            FromMonth = cbbFrom_Qui.Value.ToString();
                            EndMonth = cbbEnd_Qui.Value.ToString();
                            FromYear = cbbFromYear_Qui.Value.ToString();
                            EndYear = cbbEndYear_Qui.Value.ToString();
                        }
                        else if (ccbLoai.Value.ToString().Equals("Year"))
                        {
                            FromYear = cbbFrom_Year.Value.ToString();
                            EndYear = cbbEnd_Year.Value.ToString();
                        }
                        string strParameter = string.Format("{0},{1},{2},{3},{4},{5},{6}", reportPath, toChucId, ccbLoai.Value.ToString(), FromMonth, EndMonth, FromYear, EndYear);
                        //Check condition
                        UCReporting.LoadReport(strParameter);
                    }
                }
            }
        }
        /// <summary>
        /// Reset
        /// </summary>
        [DirectMethod]
        public void btnReset_Click()
        {
            //Set empty cho panel thay doi nhan vien
            if (hdReportType.Value.Equals("5"))
            {
                #region
                lblMessage.Hidden = true;
                lblMessageEnd_Qui.Hidden = true;
                lblMessageEnd_Thang.Hidden = true;
                lblMessageEndYear_Year.Hidden = true;
                lblMessageFrom_Thang.Hidden = true;
                lblMessageFromYear_Year.Hidden = true;
                lblMesseageForm_Qui.Hidden = true;

                cbbEnd_Qui.Clear();
                cbbEnd_Year.Clear();
                cbbEndMonth.Clear();
                cbbEndYear.Clear();
                cbbEndYear_Qui.Clear();
                cbbFrom_Qui.Clear();
                cbbFrom_Year.Clear();
                cbbFromMonth.Clear();
                cbbFromYear.Clear();
                cbbFromYear_Qui.Clear();
                cbbFromMonth.Clear();
                cbbFromMonth.Clear();
                ccbLoai.Clear();
                txtToChuc_ThayDoi.Text = string.Empty;
                #endregion

                pnlQui.Hidden = true;
                PnlThang.Hidden = true;
                pnlYear.Hidden = true;
            }
            else
            {
                txtToChuc.Text = string.Empty;
                cbbYear.Clear();
                lblMessageToChuc_NhanVien.Hidden = true;
                lblMessageYear_NhanVien.Hidden = true;
            }

            hdTochucID.Text = string.Empty;
        }
        /// <summary>
        /// Check điều kiện trong báo cáo danh sách nhân viên
        /// </summary>
        /// <returns></returns>
        private bool CheckValidateBaoCaoNhanVien()
        {
            bool check = true;
            if (string.IsNullOrEmpty(txtToChuc.Text))
            {
                lblMessageToChuc_NhanVien.Hidden = false;
                check = false;
            }
            if (string.IsNullOrEmpty(cbbYear.SelectedItem.Value))
            {
                lblMessageYear_NhanVien.Hidden = false;
                check = false;
            }
            return check;
        }
        /// <summary>
        /// Check điều kiện trong báo cáo thay đổi nhân viên
        /// </summary>
        /// <returns></returns>
        private bool CheckValidateThayDoiNhanVien()
        {
            bool check = true;
            if (ccbLoai.SelectedItem.Value != null)
            {
                if (ccbLoai.SelectedItem.Value.Equals("Month"))
                {
                    if (string.IsNullOrEmpty(cbbFromMonth.SelectedItem.Text) || string.IsNullOrEmpty(cbbFromYear.SelectedItem.Text))
                    {
                        lblMessageFrom_Thang.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMessageFrom_Thang.Hidden = true;
                    }
                    if (string.IsNullOrEmpty(cbbEndMonth.SelectedItem.Text) || string.IsNullOrEmpty(cbbEndYear.SelectedItem.Text))
                    {
                        lblMessageEnd_Thang.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMessageEnd_Thang.Hidden = true;
                    }
                }
                else if (ccbLoai.SelectedItem.Value.Equals("Qui"))
                {
                    if (string.IsNullOrEmpty(cbbFrom_Qui.SelectedItem.Text) || string.IsNullOrEmpty(cbbFromYear_Qui.SelectedItem.Text))
                    {
                        lblMesseageForm_Qui.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMesseageForm_Qui.Hidden = true;
                    }
                    if (string.IsNullOrEmpty(cbbEnd_Qui.SelectedItem.Text) || string.IsNullOrEmpty(cbbEndYear_Qui.SelectedItem.Text))
                    {
                        lblMessageEnd_Qui.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMessageEnd_Qui.Hidden = true;
                    }
                }
                else if (ccbLoai.SelectedItem.Value.Equals("Year"))
                {
                    if (string.IsNullOrEmpty(cbbFrom_Year.SelectedItem.Text))
                    {
                        lblMessageFromYear_Year.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMessageFromYear_Year.Hidden = true;
                    }
                    if (string.IsNullOrEmpty(cbbEnd_Year.SelectedItem.Text))
                    {
                        lblMessageEndYear_Year.Hidden = false;
                        check = false;
                    }
                    else
                    {
                        lblMessageEndYear_Year.Hidden = true;
                    }
                }

            }
            return check;
        }
        /// <summary>
        /// Load data cho commbox năm theo xml
        /// </summary>
        private void LoadDataComboboxLoai()
        {
            string xmlFile = Server.MapPath(ViewerConstant.FILEPATH_Report_XML);

            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            storeNam.DataSource = ds.Tables["Nam"];
            storeNam.DataBind();

            storeNam.DataSource = ds.Tables["Nam"];
            storeNam.DataBind();

            storeThang.DataSource = ds.Tables["Thang"];
            storeThang.DataBind();

            storeQui.DataSource = ds.Tables["Qui"];
            storeQui.DataBind();
        }

        [DirectMethod]
        public void ShowPopUp()
        {
            UcToChuc.ShowLookUp_ToChuc("", true, "");
        }
        [DirectMethod]
        public void SetHidden(bool pnlSearchBool, bool pnlThayDoiNhanVienBool)
        {
            btnReset_Click();
            PnlSearch.Hidden = pnlSearchBool;
            PnlSearchThayDoiNhanVien.Hidden = pnlThayDoiNhanVienBool;

        }
        /// <summary>
        /// Set hidden panel
        /// </summary>
        [DirectMethod]
        public void Disable()
        {
            string strLoai = ccbLoai.Text;
            if (ccbLoai.SelectedItem.Value != null)
            {
                if (ccbLoai.SelectedItem.Value.Equals("Month"))
                {
                    pnlQui.Hidden = true;
                    PnlThang.Hidden = false;
                    pnlYear.Hidden = true;
                }
                else if (ccbLoai.SelectedItem.Value.Equals("Qui"))
                {
                    pnlQui.Hidden = false;
                    PnlThang.Hidden = true;
                    pnlYear.Hidden = true;
                }
                else if (ccbLoai.SelectedItem.Value.Equals("Year"))
                {
                    pnlQui.Hidden = true;
                    PnlThang.Hidden = true;
                    pnlYear.Hidden = false;
                }
                else
                {
                    pnlQui.Hidden = true;
                    PnlThang.Hidden = true;
                    pnlYear.Hidden = true;
                }
            }

        }
    }
}